package com.ruoyi.vpn.service.impl;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.vpn.domain.VpnPlan;
import com.ruoyi.vpn.domain.VpnUser;
import com.ruoyi.vpn.domain.dto.AddManyChamiDTO;
import com.ruoyi.vpn.domain.dto.VpnCarmiDTO;
import com.ruoyi.vpn.service.IVpnUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.vpn.mapper.VpnCarmiMapper;
import com.ruoyi.vpn.domain.VpnCarmi;
import com.ruoyi.vpn.service.IVpnCarmiService;
import com.ruoyi.utils.MyThreadLocal;

/**
 * carmiService业务层处理
 * 
 * @author Edwin
 * @date 2024-01-08
 */
@Service
public class VpnCarmiServiceImpl implements IVpnCarmiService 
{
    @Autowired
    private VpnCarmiMapper vpnCarmiMapper;
    @Autowired
    private IVpnUserService vpnUserService;
    /**
     * 查询carmi
     * 
     * @param carmiId carmi主键
     * @return carmi
     */
    @Override
    public VpnCarmi selectVpnCarmiByCarmiId(Long carmiId)
    {
        return vpnCarmiMapper.selectVpnCarmiByCarmiId(carmiId);
    }

    /**
     * 查询carmi列表
     * 
     * @param vpnCarmi carmi
     * @return carmi
     */
    @Override
    public List<VpnCarmi> selectVpnCarmiList(VpnCarmi vpnCarmi)
    {
        return vpnCarmiMapper.selectVpnCarmiList(vpnCarmi);
    }
    /**
     * 新增carmi
     * 
     * @param vpnCarmi carmi
     * @return 结果
     */
    @Override
    public int insertVpnCarmi(VpnCarmi vpnCarmi)
    {
        vpnCarmi.setCreateTime(DateUtils.getNowDate());
        return vpnCarmiMapper.insertVpnCarmi(vpnCarmi);
    }
    /**
     * 修改carmi
     * 
     * @param vpnCarmi carmi
     * @return 结果
     */
    @Override
    public int updateVpnCarmi(VpnCarmi vpnCarmi)
    {
        vpnCarmi.setUpdateTime(DateUtils.getNowDate());
        return vpnCarmiMapper.updateVpnCarmi(vpnCarmi);
    }
    /**
     * 批量删除carmi
     * 
     * @param carmiIds 需要删除的carmi主键
     * @return 结果
     */
    @Override
    public int deleteVpnCarmiByCarmiIds(Long[] carmiIds)
    {
        return vpnCarmiMapper.deleteVpnCarmiByCarmiIds(carmiIds);
    }

    /**
     * 删除carmi信息
     * 
     * @param carmiId carmi主键
     * @return 结果
     */
    @Override
    public int deleteVpnCarmiByCarmiId(Long carmiId)
    {
        return vpnCarmiMapper.deleteVpnCarmiByCarmiId(carmiId);
    }

    @Override
    public int insertManyVpnCarmi(VpnCarmiDTO vpnCarmiDTO) {
        //获取生成数量
        Long carmiNum = vpnCarmiDTO.getCarmiNum();
        //获取套餐id
        Long planId = vpnCarmiDTO.getPlanId();
        ArrayList<String> contents = new ArrayList<>();
        for (int i = 0; i<carmiNum; i++){
            UUID uuid = UUID.randomUUID();
            contents.add(uuid.toString().replaceAll("-", ""));
        }
        AddManyChamiDTO addManyChamiDTO = new AddManyChamiDTO(contents,planId);

        return vpnCarmiMapper.insertManyVpnCarmi(addManyChamiDTO);
    }

    @Override
    public Integer rechargeCarmi(VpnCarmi vpnCarmi) {
        List<VpnCarmi> vpnCarmis = vpnCarmiMapper.selectVpnCarmiListStrict(vpnCarmi);
//        System.out.println("vpnCarmis = " + vpnCarmis.get(0));
//        System.err.println(vpnCarmis.size());
//        System.err.println(vpnCarmis.get(0).getStatus());
//        System.err.println(vpnCarmis.get(0).getUserId());
        if (vpnCarmis.size() !=0 && vpnCarmis.get(0).getStatus()!=0&& vpnCarmis.get(0).getUserId()==null){
            System.out.println("okokok");
            //给当前User设置套餐对应的流量与天数

            //获取套餐流量和天数
            VpnPlan vpnPlan = vpnCarmis.get(0).getVpnPlan();
            Long carmiId = vpnCarmis.get(0).getCarmiId();
            Long bandwidth = vpnPlan.getBandwidth();
            Long days = vpnPlan.getDays();
            VpnUser vpnUser = new VpnUser();

            //卡密单位为G 用户显示为M *102即可
            vpnUser.setBandwidth(bandwidth*1024);
            Long userId = Long.valueOf(MyThreadLocal.getData());
            vpnUser.setUserId(userId);
            vpnUser.setExpirationTime(Date.from(LocalDateTime.now().plusDays(days).atZone(ZoneId.systemDefault()).toInstant()));
            vpnUserService.updateVpnUser(vpnUser);
            //将卡密失效
            VpnCarmi tempvpnCarmi = new VpnCarmi();
            tempvpnCarmi.setUserId(userId);
            tempvpnCarmi.setStatus(0L);
            tempvpnCarmi.setUpdateTime(DateUtils.getNowDate());
            tempvpnCarmi.setCarmiId(carmiId);
            int i = vpnCarmiMapper.updateVpnCarmi(tempvpnCarmi);
            if (i ==1) return 1;
        }
        return 0;

    }
}
